package edu.pe.cayetano.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.beanutils.DynaBean;

import ws.synopsis.frmwrk.common.BResult;
import edu.pe.cayetano.framework.Configuration;
import edu.pe.cayetano.framework.DAOException;
import edu.pe.cayetano.model.bean.ExamenClinicoGeneralBean;
import edu.pe.cayetano.model.bean.FichaExtraOralBean;
import edu.pe.cayetano.model.bean.FichaIntraOralBean;
import edu.pe.cayetano.util.Utilitario;

public class FichaIntraOralDAO extends BaseDAO{

	
	
	String 					schema			= Configuration.getSchemaDB();
	
	public FichaIntraOralDAO(Connection cnx) {
		super(cnx);
	}

	public BResult buscarFichaIntraOralEspecifica(String codigo) throws Exception {

		FichaIntraOralBean 		ecgTO	 	= null;
		PreparedStatement   		pstmt 		 	= null;
		ResultSet 					rs 			 	= null;
		BResult						ret				= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" SELECT");
			query.append(" FILEHC,LABIOS,VESTIBULO,FRENILLOS,PALADAR_DURO,PALADAR_BLANDO,");
			query.append(" OROFARINGE,LENGUA,PISODELABOCA,ENCIAMARGINAL,ENCIAPAPILAR,ENCIAADHERIDA,HENDIDURAGINGIVAL");
			query.append(" FROM "+schema+"FICHA_INTRAORAL"); 
			query.append(" WHERE FILEHC=?");
			System.out.println("FichaIntraOralDAO:: buscarFichaIntraOralEspecifica:: "+query.toString());
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,codigo.substring(2, codigo.length()));
			rs=pstmt.executeQuery();
			ret = new BResult();
			if(rs.next()){
				ecgTO=new FichaIntraOralBean();
				ecgTO.setFileHC(rs.getString(1));
				ecgTO.setLabios(rs.getString(2));
				ecgTO.setVestibulo(rs.getString(3));
				ecgTO.setFrenillos(rs.getString(4));
				ecgTO.setPaladarDuro(rs.getString(5));
				ecgTO.setPaladarBlando(rs.getString(6));
				ecgTO.setOrofaringe(rs.getString(7));
				ecgTO.setLengua(rs.getString(8));
				ecgTO.setPisoDelaBoca(rs.getString(9));
				ecgTO.setEnciaMarginal(rs.getString(10));
				ecgTO.setEnciaPapilar(rs.getString(11));
				ecgTO.setEnciaAdherida(rs.getString(12));
				ecgTO.setHendiduraGingival(rs.getString(13));
				ret.setObject(ecgTO);
				ret.setCode(0);
			}else{
				ret.setCode(1);
			}
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(-1);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
				rs.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	}

	public BResult editaFichaIntraOralArchivoHC(DynaBean dyna) throws Exception {
		BResult					ret				= null;
		PreparedStatement		pstmt 		 	= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" UPDATE "+schema+"FICHA_INTRAORAL SET ");
			query.append(" LABIOS=?,VESTIBULO=?,FRENILLOS=?,PALADAR_DURO=?,");	 	
			query.append(" PALADAR_BLANDO=?,OROFARINGE=?,");		
			query.append(" LENGUA=?,PISODELABOCA=?,ENCIAMARGINAL=?,ENCIAPAPILAR=?,ENCIAADHERIDA=?,HENDIDURAGINGIVAL=?");
			query.append(" WHERE FILEHC=?"); 
			String filehc = Utilitario.getPropertyDynaBean(dyna,"filehc");
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,Utilitario.getPropertyDynaBean(dyna,"labios"));
			pstmt.setString(2,Utilitario.getPropertyDynaBean(dyna,"vestibulo"));
			pstmt.setString(3,Utilitario.getPropertyDynaBean(dyna,"frenillos"));
			pstmt.setString(4,Utilitario.getPropertyDynaBean(dyna,"paladarDuro"));
			pstmt.setString(5,Utilitario.getPropertyDynaBean(dyna,"paladarBlando"));
			pstmt.setString(6,Utilitario.getPropertyDynaBean(dyna,"orofaringe"));
			pstmt.setString(7,Utilitario.getPropertyDynaBean(dyna,"lengua"));
			pstmt.setString(8,Utilitario.getPropertyDynaBean(dyna,"pisoDelaBoca"));
			pstmt.setString(9,Utilitario.getPropertyDynaBean(dyna,"enciaMarginal"));
			pstmt.setString(10,Utilitario.getPropertyDynaBean(dyna,"enciaPapilar"));
			pstmt.setString(11,Utilitario.getPropertyDynaBean(dyna,"enciaAdherida"));
			pstmt.setString(12,Utilitario.getPropertyDynaBean(dyna,"hendiduraGingival"));
			pstmt.setString(13,filehc.substring(2,filehc.length()));
			pstmt.executeUpdate();	
			ret = new BResult();
			ret.setCode(0);
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(-1);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	} 

	public BResult insertaFichaIntraOralArchivoHC(DynaBean dyna) throws Exception {

		BResult					ret				= null;
		PreparedStatement		pstmt 		 	= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" INSERT INTO "+schema+"FICHA_INTRAORAL VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); 
			String filehc = Utilitario.getPropertyDynaBean(dyna,"filehc");
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,filehc.substring(2,filehc.length()));
			pstmt.setString(2,Utilitario.getPropertyDynaBean(dyna,"labios"));
			pstmt.setString(3,Utilitario.getPropertyDynaBean(dyna,"vestibulo"));
			pstmt.setString(4,Utilitario.getPropertyDynaBean(dyna,"frenillos"));
			pstmt.setString(5,Utilitario.getPropertyDynaBean(dyna,"paladarDuro"));
			pstmt.setString(6,Utilitario.getPropertyDynaBean(dyna,"paladarBlando"));
			pstmt.setString(7,Utilitario.getPropertyDynaBean(dyna,"orofaringe"));
			pstmt.setString(8,Utilitario.getPropertyDynaBean(dyna,"lengua"));
			pstmt.setString(9,Utilitario.getPropertyDynaBean(dyna,"pisoDelaBoca"));
			pstmt.setString(10,Utilitario.getPropertyDynaBean(dyna,"enciaMarginal"));
			pstmt.setString(11,Utilitario.getPropertyDynaBean(dyna,"enciaPapilar"));
			pstmt.setString(12,Utilitario.getPropertyDynaBean(dyna,"enciaAdherida"));
			pstmt.setString(13,Utilitario.getPropertyDynaBean(dyna,"hendiduraGingival"));
			pstmt.setInt(14,1);	
			pstmt.setString(15,"");	
			pstmt.executeUpdate();	
			ret = new BResult();
			ret.setCode(0);
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(-1);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	} 
	
}

